package com.zdc.learn.netty.proto;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;

/**
 * netty 服务端业务处理handler
 */
public class NettyServerHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        if (msg instanceof ProtoTest.Mymessage){
            ProtoTest.Mymessage.DataType dataType = ((ProtoTest.Mymessage) msg).getDataType();
            if (dataType == ProtoTest.Mymessage.DataType.StudentType){
                ProtoTest.Student student = ((ProtoTest.Mymessage) msg).getStudent();
                System.out.println("student;"+student.getId()+":"+student.getName());
            }else{
                ProtoTest.Worker worker = ((ProtoTest.Mymessage) msg).getWorker();
                System.out.println("worker;"+worker.getAge()+":"+worker.getName());
            }

        }else {
            System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@");
        }
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {


    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.out.println("服务端异常："+cause);
    }
}
